//
//  SHBankCardManagerController.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/7/13.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
#import "YBPopupMenu.h"//弹出菜单
#import "SHCompleteCustInfoContorller.h"
#import "SHAddPayBankCardController.h"//添加银行卡
#import "SHCashCardViewController.h"//新收款卡列表
#import "SHPaymentCardViewController.h"//新支付卡列表
#import "SHBankCardManagerController.h"

@interface SHBankCardManagerController ()<UIPageViewControllerDataSource,UIPageViewControllerDelegate,UIScrollViewDelegate>
@property(nonatomic,strong)NSMutableArray             *vcArray;
@property(nonatomic,strong)UIPageViewController       *pageVC;
@property(nonatomic,strong)UISegmentedControl *  segmentedControl;
@property(nonatomic,assign)NSInteger                  curPage;//指示当前页码
@property(nonatomic,strong)UIView    *titleView;
@property(nonatomic,strong)UIButton *preViousBtn;
@property(nonatomic,strong)NSArray *popTitleArrar;//弹出菜单标题


@end

@implementation SHBankCardManagerController

- (void)viewWillAppear:(BOOL)animated{
    
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}
- (NSMutableArray *)vcArray{
    
    if (!_vcArray) {
        _vcArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _vcArray;
}



- (void)viewDidLoad {
    [super viewDidLoad];
//    [SHHomeModelView checkSdPayMethodByUserSuccess:^(NSInteger status) {
//        if (status) {
//            _popTitleArrar =   @[@"银联收款卡", @"杉德收款卡"];
//        }else{
//            _popTitleArrar =   @[@"银联收款卡"];
//        }
//    }];
    
    _popTitleArrar = @[@"银联收款卡"];

    [self createNavigationItems];
    [self createSubVc];
    [self pageVC];
}
#pragma mark --- 添加头部分页
- (void)createNavigationItems{
    
    self.view.backgroundColor  = KBASEBGCOLOR;


    NSArray *segmentedData = [[NSArray alloc]initWithObjects:@"收款卡",@"付款卡",nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:segmentedData];
    segmentedControl.frame = CGRectMake( __I6Width(62.5), __I6Height(10),200, 36);
    
    //这个是设置按下按钮时的颜色
    segmentedControl.tintColor = KSHTEXTCOLOR;
    segmentedControl.selectedSegmentIndex = 0;//默认选中的按钮索引
    
    // 下面的代码实同正常状态和按下状态的属性控制,比如字体的大小和颜色等
    
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:KSHSystemFont(12),NSFontAttributeName,KSHTEXTCOLOR, NSForegroundColorAttributeName, nil];
    
    [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal];
    
    NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:KSHTEXTCOLOR forKey:NSForegroundColorAttributeName];
    
    [segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted];
    
    //设置分段控件点击相应事件
    [segmentedControl addTarget:self action:@selector(segmentedControlAction:)forControlEvents:UIControlEventValueChanged];
    
    //将分段控件添加到导航栏上
    [[self navigationItem] setTitleView:segmentedControl];
    

    
    UIBarButtonItem *bindCard = [[UIBarButtonItem alloc]initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(addBankCard:)];
    [bindCard setWidth:-5];
    [bindCard setTitleTextAttributes:@{NSFontAttributeName:KSHSystemFont(15),NSForegroundColorAttributeName:KREDCOLOR} forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = bindCard;
}


- (void)segmentedControlAction:(UISegmentedControl *)segmentedControl{
    
    NSInteger index  = segmentedControl.selectedSegmentIndex;
    //要跳转到的vc索引
    //direction：0代表前进，1代表后退
    NSInteger toPage = index ;
    [_pageVC setViewControllers:@[self.vcArray[toPage]] direction:_curPage>toPage animated:YES completion:^(BOOL finished) {
        _curPage = toPage;
    }];
    
    

    
    
    
}



#pragma mark -- 添加银行卡
- (void)addBankCard:(UIBarButtonItem *)sender{
    
    if(_curPage){//添加付款卡
        SHAddPayBankCardController *  addPayBankCadrVc =  [[SHAddPayBankCardController  alloc]init];
        addPayBankCadrVc.payCardUsingType =  SHPayCardUsingTypeAdd;
        [self.navigationController pushViewController: addPayBankCadrVc  animated:YES];
        
    }else{//添加收款卡
        [self bindingBankCard:sender];
        
    }


}


#define ICONS  @[@"motify",@"delete",@"saoyisao",@"pay"]
#pragma mark --- 绑定银行卡
- (void)bindingBankCard:(UIBarButtonItem *)sender{
    
    SHBindCardViewController *bindingBankCardVc = [[SHBindCardViewController  alloc]init];
    bindingBankCardVc.usingType = SHCashCardUsingTypeShAddCard;
    [self.navigationController pushViewController:bindingBankCardVc animated:YES];
    
}


#pragma mark - 设置子控制器
- (void)createSubVc{
    
    //收款卡
    SHCashCardViewController *cashCardVc = [[SHCashCardViewController alloc]init];
    [self.vcArray addObject:cashCardVc];
    
    //付款卡
    SHPaymentCardViewController *pamentCardVc = [[SHPaymentCardViewController alloc]init];
    [self.vcArray addObject:pamentCardVc];
    
    pamentCardVc.push = ^(UIViewController *vc){
        
        [self.navigationController pushViewController:vc animated:YES];
    };
    cashCardVc.push = ^(UIViewController *vc){
        
        [self.navigationController pushViewController:vc animated:YES];
        
    };
}
#pragma mark - 懒加载分页控制器
- (UIPageViewController *)pageVC{
    
    if (!_pageVC) {
        _pageVC = [[UIPageViewController alloc]initWithTransitionStyle:1 navigationOrientation:0 options:nil];
        _pageVC.delegate = self;
        _pageVC.dataSource = self;
        [_pageVC setViewControllers:@[self.vcArray[0]] direction:1 animated:NO completion:nil];
        
        _pageVC.view.frame = CGRectMake(0,0, self.view.frame.size.width, KScreenHeight );
        [self.view addSubview:_pageVC.view];
        
        //遍历pageVC.view的子视图，找到scrollview
        for (UIView *view in _pageVC.view.subviews) {
            if ([view isKindOfClass:[UIScrollView class]]) {
                //使用kvc赋值
                [view setValue:self forKeyPath:@"delegate"];
                ((UIScrollView *)view).scrollEnabled = NO ;
            }
        }

    }
    
    
    return _pageVC;
}


#pragma mark - PageVCDelegate
//返回下一页VC
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
    //拿到当前显示的vc对应在数组中的索引
    NSInteger index = [self.vcArray indexOfObject:viewController];
    //如果当前显示的已经是最后一页
    if (index == self.vcArray.count -1) {
        return nil;
    }
    return self.vcArray[index+1];
}
//返回上一页
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
    NSInteger index = [self.vcArray indexOfObject:viewController];
    if (index == 0) {
        return nil;
    }
    return self.vcArray[index-1];
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray *)previousViewControllers transitionCompleted:(BOOL)completed
{
    // NSLog(@"completed!");
    _curPage = [self.vcArray indexOfObject:self.pageVC.viewControllers[0]];
    //NSLog(@"%ld",_curPage);
}
#pragma mark - ScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
}




@end
